﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Core.GenericDefinitions.Delegates;
using Core.GenericDefinitions.BaseClasses;
using Core.Helpers.CommonHelpers;
using Core.Requests.SRVUICommonRequests;

namespace UI.Common
{
    public class TransportOperations : ViewModelBase
    {
        private bool _isBussy;
        public bool IsBussy
        {
            get { return _isBussy; }
            set
            {
                if (_isBussy == value) return;
                _isBussy = value;
                MainPageViewModel vm = (MainPageViewModel)App.AnaSayfa.DataContext;
                vm.IsApplicationBussy = value;
            }
        }

        public static ResponseEventHandler ResponseAction;

        //TODO: Bu send fonksyionuna birde delegate eklenecekki mesaj döndüğünde ne yapılacağı belirlensin
        public void Send(RequestBase outGoingMessage, ResponseEventHandler returnAction)
        {
            if (!(outGoingMessage is LoginRequest))
            {
                if (!ApplicationInformation.CurrentUser.IsLoggedIn)
                {
                    Operations.WarnUser("Lütfen giriş yapınız");
                    return;
                }
            }
            else if (
                ApplicationInformation.LoginInfo.UserName == ""
                || ApplicationInformation.LoginInfo.Password == ""
                )
            {
                Operations.WarnUser("Lütfen kullanıcı adı ve şifrenizi giriniz");
                return;
            }

            outGoingMessage.LoginInfo = ApplicationInformation.LoginInfo;
            ResponseAction = returnAction;
            ServiceReference1.EOrgServiceClient sc = new ServiceReference1.EOrgServiceClient();

            sc.DoWorkCompleted += new EventHandler<ServiceReference1.DoWorkCompletedEventArgs>(sc_DoWorkCompleted);

            sc.DoWorkAsync(Converter.ObjectToCommpressedByteArray(outGoingMessage));

            IsBussy = true;
        }

        void sc_DoWorkCompleted(object sender, ServiceReference1.DoWorkCompletedEventArgs e)
        {
            IsBussy = false;

            if (e.Error != null)
            {
                //TODO: servisten hata döndüğünde ne yapılacak?
                MessageBox.Show(e.Error.Message);
                return;
            }

            if (ResponseAction == null)
            {
                //TODO: responseAction null ise bunun sonucu beklenmiyor demektir


                return;
            }

            ResponseBase rb = (ResponseBase)Converter.CompressedByteArrayToObject(e.Result);

            if (rb.Error != null)
            {
                MessageBox.Show(rb.Message);
                return;
            }

            // null gelirse bu şekilde olsun
            rb = rb ?? new ResponseBase();

            ResponseAction.Invoke(rb);
            ResponseAction = null;


        }

    }
}
